package kernel_gui.gui.extended;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;

import kernel_gui.gui.generic.graphic.HallOfFame;

import common.entities.persistent.User;

@SuppressWarnings("serial")
public class EHallOfFame extends HallOfFame {

	List<User> topUsers;
	User currentUser;
	List<JLabel> idLabelList;
	List<JLabel> userNameLabelList;
	List<JLabel> gamesPlayedLabelList;
	List<JLabel> moneyWonLabelList;
	List<JLabel> personalRecordLabelList;
	
	public EHallOfFame(List<User> topUsers){
		this.topUsers = topUsers;
		currentUser = kernel.getCurrentUser();
		
		initIdLabelsToList();
		initUserNameLabelsToList();
		initGamesPlayedLabelsToList();
		initMoneyWonLabelsToList();
		initPersonalRecordLabelsToList();
		
		for(int i = 0; i < 15; i++){
			if(topUsers != null && i < topUsers.size()){
				userNameLabelList.get(i).setText(myFormat(topUsers.get(i).getName(), 58));
				gamesPlayedLabelList.get(i).setText(myFormat(String.valueOf(topUsers.get(i).getGamesPlayed()), 33));
				moneyWonLabelList.get(i).setText(myFormat(currency_nf.format(topUsers.get(i).getMoneyWon()), 33));
				personalRecordLabelList.get(i).setText(myFormat(currency_nf.format(topUsers.get(i).getPersonalRecord()), 33));
				if(topUsers.get(i).getId() == currentUser.getId()){
					userNameLabelList.get(i).setForeground(Color.red);
					gamesPlayedLabelList.get(i).setForeground(Color.red);
					moneyWonLabelList.get(i).setForeground(Color.red);
					personalRecordLabelList.get(i).setForeground(Color.red);
				}
			}else{
				userNameLabelList.get(i).setText(myFormat("< NONE >", 58));
				gamesPlayedLabelList.get(i).setText(myFormat("< NONE >", 33));
				moneyWonLabelList.get(i).setText(myFormat("< NONE >", 33));
				personalRecordLabelList.get(i).setText(myFormat("< NONE >", 33));
			}
		}
	}
	
	private String myFormat(String s, int fitTo){
		StringBuilder sb = new StringBuilder();
		fitTo = fitTo - s.length();
		for(int i = 0; i < fitTo/2; i++){
			sb.append(" ");
		}
		sb.append(s);
		for(int i = fitTo/2; i < fitTo; i++){
			sb.append(" ");
		}
		
		return sb.toString();
	}
	
	private void initIdLabelsToList(){
		idLabelList = new ArrayList<JLabel>();
		idLabelList.add(idLabel01);
		idLabelList.add(idLabel02);
		idLabelList.add(idLabel03);
		idLabelList.add(idLabel04);
		idLabelList.add(idLabel05);
		idLabelList.add(idLabel06);
		idLabelList.add(idLabel07);
		idLabelList.add(idLabel08);
		idLabelList.add(idLabel09);
		idLabelList.add(idLabel10);
		idLabelList.add(idLabel11);
		idLabelList.add(idLabel12);
		idLabelList.add(idLabel13);
		idLabelList.add(idLabel14);
		idLabelList.add(idLabel15);
	}
	
	private void initUserNameLabelsToList(){
		userNameLabelList = new ArrayList<JLabel>();
		userNameLabelList.add(userNameLabel01);
		userNameLabelList.add(userNameLabel02);
		userNameLabelList.add(userNameLabel03);
		userNameLabelList.add(userNameLabel04);
		userNameLabelList.add(userNameLabel05);
		userNameLabelList.add(userNameLabel06);
		userNameLabelList.add(userNameLabel07);
		userNameLabelList.add(userNameLabel08);
		userNameLabelList.add(userNameLabel09);
		userNameLabelList.add(userNameLabel10);
		userNameLabelList.add(userNameLabel11);
		userNameLabelList.add(userNameLabel12);
		userNameLabelList.add(userNameLabel13);
		userNameLabelList.add(userNameLabel14);
		userNameLabelList.add(userNameLabel15);
	}
	
	private void initGamesPlayedLabelsToList(){
		gamesPlayedLabelList = new ArrayList<JLabel>();
		gamesPlayedLabelList.add(gamesPlayedLabel01);
		gamesPlayedLabelList.add(gamesPlayedLabel02);
		gamesPlayedLabelList.add(gamesPlayedLabel03);
		gamesPlayedLabelList.add(gamesPlayedLabel04);
		gamesPlayedLabelList.add(gamesPlayedLabel05);
		gamesPlayedLabelList.add(gamesPlayedLabel06);
		gamesPlayedLabelList.add(gamesPlayedLabel07);
		gamesPlayedLabelList.add(gamesPlayedLabel08);
		gamesPlayedLabelList.add(gamesPlayedLabel09);
		gamesPlayedLabelList.add(gamesPlayedLabel10);
		gamesPlayedLabelList.add(gamesPlayedLabel11);
		gamesPlayedLabelList.add(gamesPlayedLabel12);
		gamesPlayedLabelList.add(gamesPlayedLabel13);
		gamesPlayedLabelList.add(gamesPlayedLabel14);
		gamesPlayedLabelList.add(gamesPlayedLabel15);
	}
	
	private void initMoneyWonLabelsToList(){
		moneyWonLabelList = new ArrayList<JLabel>();
		moneyWonLabelList.add(moneyWonLabel01);
		moneyWonLabelList.add(moneyWonLabel02);
		moneyWonLabelList.add(moneyWonLabel03);
		moneyWonLabelList.add(moneyWonLabel04);
		moneyWonLabelList.add(moneyWonLabel05);
		moneyWonLabelList.add(moneyWonLabel06);
		moneyWonLabelList.add(moneyWonLabel07);
		moneyWonLabelList.add(moneyWonLabel08);
		moneyWonLabelList.add(moneyWonLabel09);
		moneyWonLabelList.add(moneyWonLabel10);
		moneyWonLabelList.add(moneyWonLabel11);
		moneyWonLabelList.add(moneyWonLabel12);
		moneyWonLabelList.add(moneyWonLabel13);
		moneyWonLabelList.add(moneyWonLabel14);
		moneyWonLabelList.add(moneyWonLabel15);
	}
	
	private void initPersonalRecordLabelsToList(){
		personalRecordLabelList = new ArrayList<JLabel>();
		personalRecordLabelList.add(personalRecordLabel01);
		personalRecordLabelList.add(personalRecordLabel02);
		personalRecordLabelList.add(personalRecordLabel03);
		personalRecordLabelList.add(personalRecordLabel04);
		personalRecordLabelList.add(personalRecordLabel05);
		personalRecordLabelList.add(personalRecordLabel06);
		personalRecordLabelList.add(personalRecordLabel07);
		personalRecordLabelList.add(personalRecordLabel08);
		personalRecordLabelList.add(personalRecordLabel09);
		personalRecordLabelList.add(personalRecordLabel10);
		personalRecordLabelList.add(personalRecordLabel11);
		personalRecordLabelList.add(personalRecordLabel12);
		personalRecordLabelList.add(personalRecordLabel13);
		personalRecordLabelList.add(personalRecordLabel14);
		personalRecordLabelList.add(personalRecordLabel15);
	}
	
	
	@Override
	protected void BackButtonActionPerformed(ActionEvent evt) {
		openAnotherFrame(EMainMenuFrame.class, true);		
	}

	@Override
	protected void setWindowComponentsWhenCalledFromAnotherFrame() {
		/*not needed*/
	}

}
